﻿using System;
using System.Web;
using System.Web.SessionState;
using System.Configuration;
using System.Globalization;

using SBPweb.Common.Session;
using SBPweb.Common.Configuration;

namespace SBPweb.PROvibe.Business.Logic
{

    /// <summary>
    /// Summary description for CultureManager
    /// </summary>
    public class CultureManager
    {

        private static HttpSessionState pSession
        {
            get
            {
                return HttpContext.Current.Session;
            }
        }

        public static CultureInfo DefaultCulture
        {
            get
            {
                if (CookieHandler.DefaultCulture != null)
                {
                    return CultureInfo.GetCultureInfo(CookieHandler.DefaultCulture);
                }
                return AvailableCultures[0];
            }
        }

        public static CultureInfo[] AvailableCultures
        {
            get
            {
                string[] split = ConfigurationManager.AppSettings["culturesAvailable"].Split(new char[] { ';' }, StringSplitOptions.None);
                int count = split.Length;
                CultureInfo[] cultures = new CultureInfo[count];
                for (int i = 0; i < count; i++)
                {
                    cultures[i] = new CultureInfo(split[i]);
                }
                return cultures;
            }
        }

        private const string KEY_SelectedCulture = "SelectedCulture";
        public static CultureInfo SelectedCulture
        {
            get
            {
                if (SessionHandler.Get<CultureInfo>(KEY_SelectedCulture) == null)
                {
                    SessionHandler.Put<CultureInfo>(KEY_SelectedCulture, DefaultCulture);
                }
                return SessionHandler.Get<CultureInfo>(KEY_SelectedCulture);
            }
        }

        public static void SetSelectedCulture(string name)
        {
            SessionHandler.Put<CultureInfo>(KEY_SelectedCulture, CultureInfo.GetCultureInfo(name));
            CookieHandler.DefaultCulture = name;
        }

        public static void MaintainCulture()
        {
            System.Threading.Thread.CurrentThread.CurrentCulture = SelectedCulture;
            System.Threading.Thread.CurrentThread.CurrentUICulture = SelectedCulture;
        }

    }

}
